package servlet;

import bean.Customer;
import dao.entity.CustomerDaoImpl_2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "customerQueryServlet", value = "/cquery")
public class CustomerQueryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取数
        String cid = request.getParameter("cid");
        String page = request.getParameter("currentPage");
        int currentPage = 1;//默认的当前页码为1
        if(page!=null){
            currentPage= Integer.parseInt(page);
            request.setAttribute("currentPage",currentPage);
        }
        CustomerDaoImpl_2 cdi = new CustomerDaoImpl_2();//创建实现类的对象
        List<Customer> list = null;
        //查询全部
        list = cdi.customer_find(currentPage);
        int n = cdi.customer_count();//记录数
        int pages = (int) Math.ceil(n/5.0);//计算页数,向上取整4.1-->5.0
        request.setAttribute("cid",cid);
        request.setAttribute("list",list);
        request.setAttribute("pages",pages);
        request.getRequestDispatcher("customer.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
